﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using LockImage.Encryption;
using LockImage.Gui.Dialog;
using LockImage.Utils;

namespace LockImage.Gui
{
    public partial class MainForm : Form
    {
        #region Actions

        private void openImage()
        {
            try
            {
                this.openFileDialog = new OpenFileDialog();
                this.openFileDialog.Filter = "*.jpg,*.jpeg|*.jpg;*.jpeg|*.bmp|*.bmp|*.png|*.png|*.gif|*.gif";
                this.openFileDialog.FilterIndex = 1;
                if (DialogResult.OK == this.openFileDialog.ShowDialog())
                {
                    this.showImageFileInPicBox(this.openFileDialog.FileName);
                    this.activateMenu();
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.StackTrace);
            }
        }

        private void saveImage()
        {
            this.saveFileDialog = new SaveFileDialog();
            this.saveFileDialog.Filter = "*.exe|*.exe";
            DialogResult dialogResult = this.saveFileDialog.ShowDialog(this);
            switch (dialogResult)
            {
                case DialogResult.OK:
                    ChoosePasswordDialog choosePasswordDialog = new ChoosePasswordDialog(this);
                    choosePasswordDialog.Show();

                    break;

                case DialogResult.Cancel:
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="password"></param>
        public void SaveTo(String password)
        {
            File.Copy(MainApplication.THIS_EXECUATABLE_FULL_PATH, this.saveFileDialog.FileName, true);
            byte[] imgAsByteArr = (byte[])(new ImageConverter().ConvertTo(this.pictureBox.Image, typeof(byte[])));

            IEncryptionEngine encryption = new RijndaelEncryptionEngine();
            byte[] enryptedImgAsByteArr = encryption.Encrypt(imgAsByteArr, password);

            String enryptedImgAsFile = Path.GetTempFileName();
            IOUtils.WriteToFile(new MemoryStream(enryptedImgAsByteArr), enryptedImgAsFile);

            ResourceManager.UpdateResource(this.saveFileDialog.FileName, enryptedImgAsFile);

            File.Delete(enryptedImgAsFile);
        }

        private void zoomIn()
        {
            this.pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
            this.pictureBox.Width = (int)(this.pictureBox.Width * zoomRatio);
            this.pictureBox.Height = (int)(this.pictureBox.Height * zoomRatio);
        }

        private void zoomOut()
        {
            this.pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
            this.pictureBox.Width = (int)(this.pictureBox.Width / zoomRatio);
            this.pictureBox.Height = (int)(this.pictureBox.Height / zoomRatio);
        }

        private void showInfo()
        {
            MessageBox.Show(Constants.ABOUT);
        }

        private void exit()
        {
            Application.Exit();
        }

        private void activateMenu()
        {
            this.zoominButton.Enabled = true;
            this.zoomOutButton.Enabled = true;
            this.zoomInToolStripMenuItem.Enabled = true;
            this.zoomOutToolStripMenuItem.Enabled = true;
            this.saveAsToolStripMenuItem.Enabled = true;
            this.imgSaveButton.Enabled = true;
        }

        public void ShowImage(Image image)
        {
            this.pictureBox.Image = image;
            this.pictureBox.Width = this.pictureBox.Image.Width;
            this.pictureBox.Height = this.pictureBox.Image.Height;
            base.Size = new System.Drawing.Size(this.pictureBox.Width, this.pictureBox.Height);
            this.activateMenu();
        }

        private void showImageFileInPicBox(String fileName)
        {
            if (!this.IsImage(fileName))
            {
                MessageBox.Show("The file you are trying to open is not an image!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                Image img = Image.FromFile(fileName);
                this.pictureBox.Image = img;
                this.pictureBox.Width = this.pictureBox.Image.Width;
                this.pictureBox.Height = this.pictureBox.Image.Height;
                this.Size = new System.Drawing.Size(this.pictureBox.Width, this.pictureBox.Height);
                this.saveAsToolStripMenuItem.Enabled = true;
            }
        }

        #endregion Actions

        #region Helper Methods

        /// <summary>
        ///
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        private bool IsImage(Stream stream)
        {
            Image img;
            bool isImg = true;
            try
            {
                img = Image.FromStream(stream);
            }
            catch (Exception ex)
            {
                isImg = false;
            }
            img = null;
            return isImg;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="imageFileName"></param>
        /// <returns></returns>
        private bool IsImage(String imageFileName)
        {
            FileStream filestream = new FileStream(imageFileName, FileMode.Open, FileAccess.Read);
            bool isImg = this.IsImage(filestream);
            filestream.Close();
            filestream = null;
            return isImg;
        }

        #endregion Helper Methods
    }
}